package org.example.springbook.service;

import org.example.springbook.dao.BookDao;
import org.example.springbook.enums.BookStatusEnums;
import org.example.springbook.mapper.BookMapper;
import org.example.springbook.model.BookInfo;
import org.example.springbook.model.PageRequest;
import org.example.springbook.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
// 服务层
import java.util.List;

@Service
public class BookService {
    @Autowired
    private BookDao bookDao;
    @Autowired
    private BookMapper bookMapper;

    public List<BookInfo> getBookList() {
        // 对数据进行处理：状态转换
        List<BookInfo> books = bookMapper.queryBookList();
//        List<BookInfo> books = bookDao.mockBook();
        for(BookInfo book : books) {
            if(book.getStatus() == 1) {
                book.setStatusCN("可借阅");
            } else if(book.getStatus() == 2) {
                book.setStatusCN("不可借阅");
            }
        }
        return books;
    }

    public Integer insertBook(BookInfo book) {
        return bookMapper.insertBook(book);
    }

    public PageResult<BookInfo> getListByPage(PageRequest pageRequest) {
        // 查询记录的总数
        Integer count = bookMapper.count();
        // 查询当前页的数据
        List<BookInfo> bookInfos = bookMapper.queryBookListByPage(pageRequest);
        for(BookInfo book : bookInfos) {
            // 根据状态，设置描述
            book.setStatusCN(BookStatusEnums.getDescByCode(book.getStatus()).getDesc());
        }
        return new PageResult<>(bookInfos, count, pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookMapper.queryBookById(bookId);
    }

    public Integer updateBook(BookInfo book) {
        return bookMapper.updateBook(book);
    }


    public Integer batchDelete(List<Integer> ids) {
        return bookMapper.batchDelete(ids);
    }
}
